<?php
class Training_DbController extends Zendvn_Controller_Action{
    
    public function init() {
        parent::init();
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function indexAction() {
        echo __METHOD__;
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        
        echo $mode = Zend_Db::FETCH_NUM;
        echo $mode = Zend_Db::FETCH_ASSOC;
        echo $mode = Zend_Db::FETCH_OBJ;
        echo $mode = Zend_Db::FETCH_BOTH;
        $db->setFetchMode($mode);
        $sql = "SELECT * FROM user_group";
        $rows   = $db->fetchAll($sql);
        
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index2Action() {
        echo __METHOD__ . "FETCH ALL";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        $sql = "SELECT * FROM user_group WHERE group_acp = ? AND group_default = ?";
        $bind = array(0,0);
        $rows   = $db->fetchAll($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index3Action() {
        echo __METHOD__ . "FETCH ALL";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        echo $mode = Zend_Db::FETCH_OBJ;
        $db->setFetchMode($mode);
        
        $sql = "SELECT * FROM user_group WHERE group_acp = ? AND group_default = ?";
        $bind = array(0,0);
        $rows   = $db->fetchAssoc($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index4Action() {
        echo __METHOD__ . "FETCH ALL";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        echo $mode = Zend_Db::FETCH_OBJ;
        $db->setFetchMode($mode);
        
        $sql = "SELECT group_name, avatar FROM user_group WHERE group_acp = ?";
        $bind = array(1);
        $rows   = $db->fetchCol($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index5Action() {
        echo __METHOD__ . "fetch Pairs";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        echo $mode = Zend_Db::FETCH_OBJ;
        $db->setFetchMode($mode);
        
        $sql = "SELECT group_name, avatar FROM user_group WHERE group_acp = ?";
        $bind = array(1);
        $rows   = $db->fetchPairs($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index6Action() {
        echo __METHOD__ . "fetch Pairs";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        echo $mode = Zend_Db::FETCH_OBJ;
        $db->setFetchMode($mode);
        
        $sql = "SELECT id, group_name FROM user_group WHERE group_acp = ? ORDER BY group_name DESC";
        $bind = array(1);
        $rows   = $db->fetchRow($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function index7Action() {
        echo __METHOD__ . "fetchOne";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        echo $mode = Zend_Db::FETCH_OBJ;
        $db->setFetchMode($mode);
        
        $sql = "SELECT COUNT(id) FROM user_group";
        $bind = array(1);
        $rows   = $db->fetchOne($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function insertAction() {
        echo "<br><h3>".__METHOD__ . " - insert() </h3>";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        $table  = "user_group";
        $bind = array(
                        'group_name' => 'Guest 123',
                        'avatar' => 'Guest.png',
                        'ranking' => 'rGuest.png',
                        'group_acp' => 0,
                        'group_default' => 1,
                        'status' => 1,
                        'order' => 10,
                    );
        $db->insert($table, $bind);
    }
    
    public function lastIdAction() {
        echo "<br><h3>".__METHOD__ . " - lastId() </h3>";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        $table  = "user_group";
        $primaryKey = "id";
        $bind = array(
                        'group_name' => 'Guest 456',
                        'avatar' => 'Guest.png',
                        'ranking' => 'rGuest.png',
                        'group_acp' => 0,
                        'group_default' => 1,
                        'status' => 1,
                        'order' => 10,
                    );
        $db->insert($table, $bind);
        echo "<br/>" . $db->lastInsertId($table, $primaryKey);
    }
    
    public function updateAction() {
        echo "<br><h3>".__METHOD__ . " - update() </h3>";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        $table  = "user_group";
        $primaryKey = "id";
        $bind = array(
                        'group_name' => 'New Guest',
                        'avatar' => 'Guest.png',
                        'ranking' => 'rGuest.png',
                        'group_acp' => 0,
                        'group_default' => 0,
                        'status' => 0,
                        'order' => 100,
                    );
         $where = " group_name = 'Guest 123'";
        $db->update($table, $bind, $where);
    }
    
    public function selectAction() {
        echo "<br><h3>".__METHOD__ . " - select() </h3>";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        
        $id     = $db->quote($this->_request->getParam('groupID'), INTEGER);
        $sql    = "SELECT * FROM user_group WHERE id = ?";
        $bind   = array($id);
        $rows   = $db->fetchAll($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
    
    public function select2Action() {
        echo "<br><h3>".__METHOD__ . " - select() </h3>";
        $db     = Zend_Registry::get('connectDb');
//        $db     = Zend_Db::factory($adapter, $config);
        
        echo "<br>" . $gname  = $db->quote($this->_request->getParam('gname'), STRING);
        $sql    = "SELECT * FROM user_group WHERE group_name = " . $gname;
        $bind   = null;
        $rows   = $db->fetchAll($sql, $bind);
        echo '<pre>';
        print_r($rows);
        echo '<pre>';
    }
}
